package ControlLayer;
import java.util.ArrayList;

import DBLayer.DBConnection;
import DBLayer.DBSalesOrder;
import DBLayer.IFDBSalesOrder;
import ModelLayer.Customer;
import ModelLayer.Employee;
import ModelLayer.Product;
import ModelLayer.SalesOrder;
import ModelLayer.SalesOrderLine;
import ModelLayer.Store;

public class SalesOrderCtr 
{
	private SalesOrder salesorderObj;
	
	public SalesOrderCtr()
	{
		
	}
	
	//This method returns all the customers salesOrder on their phoneNumber in an Arraylist.
	public ArrayList<SalesOrder> findAllSalesOrderOnCustomer(int phoneNumber)
	{
		IFDBSalesOrder dbSalesOrder = new DBSalesOrder();
		ArrayList<SalesOrder> allSalesOrder = new ArrayList<SalesOrder>();
		allSalesOrder = dbSalesOrder.findAllSalesOrdersOnCustomer(phoneNumber);
		return allSalesOrder;
	}
	
	//This method returns all the employees salesOrder on their phoneNumber in an Arraylist.
	public ArrayList<SalesOrder> findAllSalesOrderOnEmployee(int phoneNumber)
	{
		IFDBSalesOrder dbSalesOrder = new DBSalesOrder();
		ArrayList<SalesOrder> allSalesOrder = new ArrayList<SalesOrder>();
		allSalesOrder = dbSalesOrder.findAllSalesOrdersOnEmployee(phoneNumber);
		return allSalesOrder;
	}
	
	//This method search for a sales order on saleID.
	public SalesOrder findSalesOrder(int SaleId)
	{
		IFDBSalesOrder dbSalesOrder = new DBSalesOrder();
		return dbSalesOrder.findSalesOrder(SaleId, true);
	}
	
	//This method create a new SalesOrder in DBSalesOrder with date, deliverystatus, deliverydate ?.
	public void insertSalesOrder(String date, String orderType, String deliverystatus, String deliverydate, Customer phoneNumber, Employee empPhone)
	{
		salesorderObj = new SalesOrder();
		salesorderObj.setDate(date);
		salesorderObj.setOrderType(orderType);
		salesorderObj.setDeliveryStatus(deliverystatus);
		salesorderObj.setDeliveryDate(deliverydate);
		salesorderObj.setCustomer(phoneNumber);
		salesorderObj.setEmployee(empPhone);
		System.out.println("lala");


	}
	
	//Method used to confirm order
	public void confirmOrder()
	{
		try{
			DBConnection.startTransaction();
			DBSalesOrder dbSalesorder = new DBSalesOrder();
			dbSalesorder.insertSalesOrder(salesorderObj);
			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}
	}
	
	//Inserts a sales order line
	public void insertSalesOrderLine(int amount, Product product, Store store)
	{
		SalesOrderLine salesLineObj = new SalesOrderLine();
		salesLineObj.setAmount(amount);
		salesLineObj.setProduct(product);
		salesLineObj.setSale(salesorderObj);
		salesLineObj.setStore(store);
		salesorderObj.addSOL(salesLineObj);
		System.out.println(store.getPhoneNumber());
	}
	
	//This method delete a salesOrder on SalesOrderId in DBSalesOrder.
	public void deleteSalesOrder(int salesOrderId)
	{
		DBSalesOrder dbSale = new DBSalesOrder();
		dbSale.deleteSalesOrder(salesOrderId);	
	}
	
}
